package com.example.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.dao.OrderDao;
import com.example.demo.pojo.CarGood;
import com.example.demo.pojo.Order;

@RestController
public class OrderController {
	@Autowired
	OrderDao orderDao;
    @PostMapping("addOrder")
    public Map addOrder(@RequestBody List<Order> orders) {
    	System.out.println(orders);
    	String result="no";
    	Map m=new HashMap();
    	try {
    		for(Order order:orders)
    			orderDao.save(order);
    		result="yes";
    	}catch(Exception e){
    		throw e;
    	}
    	m.put("result", result);
    	return m;
    }
    @GetMapping("handleOrder")
    public Map handleOrder(String id) {
    	System.out.println(id);
    	String userId=new String();
    	String goodId=new String();
    	String result="no";
    	Map m=new HashMap();
    	try {
    		orderDao.updateStateById(id);
    		result="yes";
    	}catch(Exception e){
    		throw e;
    	}
    	m.put("result", result);
    	return m;
    }
    @GetMapping("getAllOrder")
    public Map<String, List<Order>> getAllOrder(String id) {
    	System.out.println(id);
		List<Order> all= orderDao.findByUserId(id);
		Map<String,List<Order>> map=new HashMap<String,List<Order>>();
		map.put("list", all);
		System.out.println(map.toString());
		return map;
    }

}
